home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-15 | 12.1 KB | 344 lines | [TEXT/gamI] |
- ; ----------------------------------------------------------------------------
- ; File: Utilities.scm
- ; Description: Assorted utility functions.
- ; Author: Raymond Laning at ART
- ; Created: 28-Apr-93
- ; Modified: 07-Dec-93 23:18:51 Raymond Laning
- ; Language: Scheme
- ; Status: Experimental (Do Not Distribute)
- ;
- ; (c) Copyright 1993, Advanced Robotic Technologies, Inc.
- ; All Rights Reserved.
- ;
- ; ----------------------------------------------------------------------------
-
- (define $pi 3.141592653589793)
-
- (define $close-enough .00001)
-
- (define (close-enough? arg1 arg2)
- (< (- arg1 arg2) $close-enough))
-
- (define (atom? x1)
- (not (list? x1))
- )
-
- (define (precision num places)
- (let* (
- (mnum (* num (expt 10 places)))
- (rnum (round mnum))
- )
- (/ rnum (expt 10 places)))
- )
-
- (define (sqrd arg) (* arg arg))
-
- (define (factorial number)
- (if (> number 1)
- (do ((i (- number 1) (- i 1))
- (result number (* result i)))
- ((<= i 1) result))
- 1))
-
- (define (binomial-coeff n i)
- (/ (factorial n) (factorial i) (factorial (- n i))))
-
- (define (distance-sqrd v1 v2)
- (apply + (map (lambda (x1 x2) (let ((diff (- x1 x2))) (* diff diff))) v1 v2)))
-
- ;;;integer-coerce takes a number and returns the rounded integer
- ;;;(would not be necessary if round returned a TRUE integer in MacGambit)
- (define (integer-coerce num)
- (inexact->exact (round num)))
-
- (define (make-counter)
- (let ((count 0))
- (lambda () (set! count (+ 1 count)) count)))
-
- (define counter (make-counter))
-
- (define (degrees-to-radians angle)
- (/ (* $pi angle) 180))
-
- (define (radians-to-degrees angle)
- (* 180.0 (/ angle $pi)))
-
- (define (format port format-string . restargs)
- (let ((len (string-length format-string)))
- (if (and (not (number? port)) (not (output-port? port)))
- (set! port ##stdout))
- (do ((i 0))
- ((>= i len))
- (case (string-ref format-string i)
- ((#\~)
- (case (string-ref format-string (+ i 1))
- ((#\a #\A)
- (display (car restargs) port)
- (set! restargs (cdr restargs))
- (set! i (+ i 2)))
- ((#\s #\S)
- (write (car restargs) port)
- (set! restargs (cdr restargs))
- (set! i (+ i 2)))
- ((#\d #\D)
- (##write-string (##number->string (car restargs) 10) port)
- (set! restargs (cdr restargs))
- (set! i (+ i 2)))
- ((#\%)
- (newline port)
- (set! i (+ i 2)))
- ((#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
- (print-number-delimited
- (number->string (car restargs))
- (- (char->integer (string-ref format-string (+ i 1))) 48)
- port)
- (set! restargs (cdr restargs))
- (set! i (+ i 2)))
- (else (write "Bad format character ")
- (write-char (string-ref format-string i))
- (write " at position")
- (write i)
- (newline))))
- (else
- (write-char (string-ref format-string i) port)
- (set! i (+ i 1))))))
- #f)
-
- (define (string-member? char string)
- (do ((i 0 (+ i 1))
- (done #f))
- ((or done (>= i (string-length string))) (if done (- i 1) done))
- (if (char-ci=? (string-ref string i) char)
- (set! done #t))))
-
- (define (string-right-trim string trimchar)
- (let ((len (- (string-length string) 1)))
- (do ((i len (- i 1))
- (done #f))
- ((or done (< i 0))
- (if done (substring string (+ i 2) (+ 1 len)) string))
- (if (char=? trimchar (string-ref string i))
- (set! done #t)))))
-
- (define (string-downcase string)
- (let ((len (string-length string)))
- (do ((i 0 (+ i 1))
- (newstring (string-copy string)))
- ((>= i len) newstring)
- (string-set! newstring i (char-downcase (string-ref string i))))))
-
- (define (print-number-delimited number-string stlength port)
- (let ((strlen (string-length number-string))
- (exponential? (string-member? #\e number-string)))
- (cond ((and (> stlength strlen) (string-member? #\. number-string))
- (set! number-string (string-append
- number-string
- (make-string (- stlength strlen) #\0))))
- ((> stlength strlen)
- (set! number-string (string-append
- number-string
- "."
- (make-string (- stlength strlen 1) #\0)))))
- (if exponential?
- (##write-string (string-append
- (substring number-string 0 (- stlength (- strlen exponential?)))
- (substring number-string exponential? strlen))
- port)
- (##write-string (substring number-string 0 stlength) port))))
-
- (define (do-load-file foo)
- (let ((filename (mac#sfgetfile "Select file to load" "TEXTgamO")))
- (if filename
- (begin
- (##display "Loading " ##stdout #f)
- (##write filename ##stdout #f)
- (##newline ##stdout)
- (##load filename #f)))))
-
- (define (do-compile-file foo)
- (let ((filename (mac#sfgetfile "Select file to compile" "TEXT")))
- (if filename
- (begin
- (##display "Compiling " ##stdout #f)
- (##write filename ##stdout #f)
- (##newline ##stdout)
- (if (##procedure? c#cf)
- (c#cf filename 'm68000)
- (dylan-call dylan:load filename))))))
-
- (define current-ids
- (list (list (mac#getmenu 134) "Special"
- (list (list #f "Load..." do-load-file)
- (list #f "Compile..." do-compile-file)))))
-
- (define current-subids #f)
-
- (define current-menu-id 145)
-
- (define (add-to-menu-list menuhandle name toplevel?)
- (cond (toplevel?
- (mac#insertmenu menuhandle 0)
- (set! current-ids (append current-ids (list (list menuhandle name))))
- (mac#drawmenubar)
- (- (length current-ids) 1))
- (#t
- (mac#insertmenu menuhandle -1)
- (set! current-subids
- (append current-subids (list (list menuhandle name))))
- (mac#drawmenubar)
- (- (length current-subids) 1)
- )))
-
- (define (add-new-menu name)
- (let* ((mh (mac#newmenu current-menu-id name)))
- (set! current-menu-id (+ current-menu-id 1))
- (add-to-menu-list mh name #t)))
-
- (define (add-old-menu name menuid)
- (let ((menuhandle (mac#getmenu menuid)))
- (mac#insertmenu menuhandle 0)
- (set! current-ids (append current-ids (list (list menuhandle name))))))
-
- (define (do-men-selection selection)
- (let* ((menu (mac#getmhandle (##fixnum.ash selection -16)))
- (item (##fixnum.logand selection 65535))
- (menurecord (assoc menu current-ids)))
- (if menurecord
- (if (<= item (length (caddr menurecord)))
- (apply (caddr (list-ref (caddr menurecord) (- item 1))) (list item))
- (format #t "Bogus choice number ~s~%" item))
- (let ((newmenurec (assoc menu current-subids)))
- (if newmenurec
- (apply (caddr (list-ref (caddr newmenurec) (- item 1))) (list item))
- (format #t "Bogus menu # ~s~%" menu))))
- (mac#hilitemenu 0))
- ##unprint-object)
-
- (define $menu-separator "Separator")
-
- (define (add-menu-item whichmenu itemtitle action hot-key has-subs? sep?)
- (if (< whichmenu (length current-ids))
- (let* ((menurecord (list-ref current-ids whichmenu))
- (submenurecord (cddr menurecord))
- (menuhan (car menurecord))
- (newmh #f))
- (cond (has-subs?
- (let* ((submenu-thing "!ê/ ")
- (no-thing (string-set! submenu-thing 3 (integer->char 27)))
- (no-thing2
- (string-set!
- submenu-thing 1 (integer->char current-menu-id)))
- (newtitle (string-append itemtitle submenu-thing)))
- (set! newmh (mac#newmenu current-menu-id itemtitle))
- (set! current-menu-id (+ current-menu-id 1))
- (mac#appendmenu menuhan newtitle)
- (add-to-menu-list newmh itemtitle #f)
- ))
- (hot-key
- (let* ((sub-thing "/"))
- (if sep?
- (set! sub-thing
- (string-append "-;" itemtitle sub-thing hot-key))
- (set! sub-thing (string-append itemtitle sub-thing hot-key)))
- (mac#appendmenu menuhan sub-thing)))
- (sep? (mac#appendmenu menuhan (string-append "-;" itemtitle)))
- (#t (mac#appendmenu menuhan itemtitle)))
- (cond ((and (null? submenurecord) sep?) ;first subitem, w/seperator
- (set-cdr! (cdr menurecord)
- (list (list $menu-separator
- (list newmh itemtitle action))))
- 1)
- ((null? submenurecord) ;first subitem
- (set-cdr! (cdr menurecord)
- (list (list (list newmh itemtitle action))))
- 0)
- (sep? (set-cdr! (list-tail (car submenurecord)
- (- (length (car submenurecord)) 1))
- (list $menu-separator
- (list newmh itemtitle action)))
- (- (length (car submenurecord)) 1))
- (#t (set-cdr! (list-tail (car submenurecord)
- (- (length (car submenurecord)) 1))
- (list (list newmh itemtitle action)))
- (- (length (car submenurecord)) 1))))
- (error "Not a valid menu id to add item to:" whichmenu itemtitle)))
-
- (define (add-menu-separator whichmenu)
- (let* ((menurecord (list-ref current-ids whichmenu))
- (menuhan (car menurecord)))
- (mac#appendmenu menuhan ";")))
-
- (define (add-submenu-item menuid itemid subitemtitle action)
- (let* ((menurec (list-ref current-ids menuid))
- (itemrec (list-ref (caddr menurec) itemid))
- (menuhan (car itemrec))
- (subitem (assoc menuhan current-subids))
- (subitemlist (cddr subitem)))
- (if (null? subitemlist)
- (set-cdr! (cdr subitem) (list (list (list #f subitemtitle action))))
- (set-cdr! (list-tail (car subitemlist) (- (length (car subitemlist)) 1))
- (list (list #f subitemtitle action))))
- (mac#appendmenu menuhan subitemtitle)))
-
- (define (disable-menitem menunum item)
- (let* ((menurec (list-ref current-ids menunum))
- (menuhan (car menurec)))
- (mac#disableitem menuhan item)))
-
- (define (disable-submenitem menunum submenunum item)
- (let* ((menurec (list-ref current-ids menunum))
- (itemrec (list-ref (caddr menurec) submenunum))
- (menuhan (car itemrec)))
- (mac#disableitem menuhan item)))
-
-
- (define (enable-menitem menunum item)
- (let* ((menurec (list-ref current-ids menunum))
- (menuhan (car menurec)))
- (mac#enableitem menuhan item)))
-
- (define (check-menitem menunum item flag)
- (let* ((menurec (list-ref current-ids menunum))
- (menuhan (car menurec)))
- (mac#checkitem menuhan item flag)))
-
- (define (check-submenitem menunum submenunum item flag)
- (let* ((menurec (list-ref current-ids menunum))
- (itemrec (list-ref (caddr menurec) submenunum))
- (menuhan (car itemrec)))
- (mac#checkitem menuhan item flag)))
-
- (define (butlast . args)
- (let* ((thelist (car args))
- (endnum (if (null? (cdr args))
- (- (length thelist) 2)
- (- (length thelist) (cadr args) 1))))
- (do ((i endnum (- i 1))
- (outlist (list)))
- ((negative? i) outlist)
- (set! outlist (cons (list-ref thelist i) outlist)))))
-
- (define (last thelist)
- (car (list-tail thelist (- (length thelist) 1))))
-
- (define (remove member list)
- (do ((restlist list (cdr restlist))
- (i 0 (+ i 1))
- (done? #f))
- ((or done? (null? restlist)) list)
- (cond ((and (zero? i) (equal? member (car restlist)))
- (set! done? #t)
- (set! list (cdr list)))
- ((equal? member (car restlist))
- (set! done? #t)
- (set-cdr! (list-tail list (- i 1)) (cdr restlist))))))
-
- (##define-macro (decrement foo)
- `(let ((num ,(- (eval foo) 1))) (set! ,foo num) num))
-
- (##define-macro (increment foo)
- `(let ((num ,(+ (eval foo) 1))) (set! ,foo num) num))
-
- (define (signums tesnum othernum)
- (if (negative? tesnum) (- othernum) othernum))
-